//
//  GuideView.m
//  u76ho
//
//  Created by m-air-01 on 16/2/4.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "GuideView.h"

@implementation GuideView
- (UIView *)initWithFrame:(CGRect)frame andPhotoArray:(NSArray *)photoArray
{
    self = [super initWithFrame:frame];
    if (self) {
        src = [[UIScrollView alloc]init];
        src.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        src.contentSize = CGSizeMake(SCREEN_WIDTH*photoArray.count, SCREEN_HEIGHT);
        src.bounces = NO;
        src.pagingEnabled = YES;
        src.delegate = self;
        src.showsHorizontalScrollIndicator = NO;
        src.showsVerticalScrollIndicator = NO;
        [self addSubview:src];
        
        for (int i = 0; i<photoArray.count;i++) {
            UIImageView * top = [[UIImageView alloc]init];
            top.userInteractionEnabled = YES;
            top.image = [UIImage imageNamed:[photoArray objectAtIndex:i]];
            top.frame = CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            [src addSubview:top];
            if (i == photoArray.count-1) {
                UIButton * ty_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
                if(iPhone4)
                {

                    ty_Btn.frame=CGRectMake((SCREEN_WIDTH-125)/2,SCREEN_HEIGHT-65, 125,50);
                }else if(iPhone5)
                {
                    ty_Btn.frame=CGRectMake((SCREEN_WIDTH-125)/2,SCREEN_HEIGHT-65, 125,50);

                }else if(iPhone6)
                {
                    ty_Btn.frame=CGRectMake((SCREEN_WIDTH-125)/2,SCREEN_HEIGHT-100, 125,50);

                }else if(iPhone6P)
                {
                    ty_Btn.frame=CGRectMake((SCREEN_WIDTH-125)/2,SCREEN_HEIGHT-110, 125,50);

                }
                else
                {
                    ty_Btn.frame=CGRectMake((SCREEN_WIDTH-125)/2,SCREEN_HEIGHT-110, 125,50);
                }
                ty_Btn.layer.cornerRadius = 5;
                [ty_Btn setTitle:@"立即体验" forState:UIControlStateNormal];
                ty_Btn.titleLabel.font = [UIFont systemFontOfSize:15];
                [ty_Btn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
                ty_Btn.backgroundColor = UIColorFromRGB(0xf6f6f6);
                [ty_Btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
                [top addSubview:ty_Btn];

            }
        }
        
    }
    return self;
}
-(void)buttonClick
{
    [UIView beginAnimations:@"split" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    if ([animationID isEqualToString:@"split"] && finished) {
        
        CATransition *animation = [CATransition animation];
        //animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.duration=1.0f;
        animation.delegate=self;
        self.hidden=YES;
        [self.layer addAnimation:animation forKey:@"animation"];
        
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:0];
    }
}
-(void)dismiss
{
    [self removeFromSuperview];
    if (_guideFinish) {
        self.guideFinish();
    }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
